package com.pai4j.pgc.service.es;
import com.pai4j.common.listener.event.data.CommunityEventData;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.pgc.pojo.es.CommunitySearchResultDTO;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class CommunityElasticSearchService extends AbstractElasticSearchClient<CommunitySearchResultDTO> {


    @Value("${community-index}")
    private String chatMessageIndex;

    @Override
    protected String getIndexName() {
        return chatMessageIndex;
    }

    @Override
    protected CommunitySearchResultDTO convert(String dataJson) {
        CommunitySearchResultDTO communitySearchResultDTO = new CommunitySearchResultDTO();
        CommunityEventData data = JsonUtil.fromJson(dataJson, CommunityEventData.class);
        communitySearchResultDTO.setData(data);
        return communitySearchResultDTO;
    }

    @Override
    protected void setSearchResultExtInfo(List<CommunitySearchResultDTO> dataList) {

    }

    @Override
    protected XContentBuilder getXContentBuilder() throws IOException {
        return JsonXContent.contentBuilder()
                .startObject()
                .startObject("properties")
                .startObject("path_id")
                .field("type", "long")
                .endObject()
                .startObject("type")
                .field("type", "integer")
                .endObject()
                .startObject("biz_id")
                .field("type", "keyword")
                .endObject()
                .startObject("author")
                .field("type", "keyword")
                .endObject()
                .startObject("data_json")
                .field("type", "text")
                .endObject()
                .startObject("last_date")
                .field("type", "date")
                .field("format", "yyyy-MM-dd")
                .endObject()
                .endObject()
                .endObject();
    }
}
